package com.skhu.as.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.skhu.as.domain.DutyHours;
import com.skhu.as.domain.RequestType;
import com.skhu.as.service.DutyHoursS;
import com.skhu.as.service.RequestTypeS;

public class RequestTypeC extends SimpleFormController {

	private RequestTypeS requestTypeS;
	
	public void setRequestTypeS(RequestTypeS requestTypeS) {
		this.requestTypeS = requestTypeS;
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {

		if(!isFormSubmission(request)){
			boolean def = ServletRequestUtils.getBooleanParameter(request, "default", false);
			int result = 0;
			
			if(def){
				result = requestTypeS.deleteAllRequestType();
			}
		}
		
		return super.formBackingObject(request);
	}
	
	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
		List<RequestType> requestTypeList = requestTypeS.getRequestTypeList();
		Map ref = new HashMap();
		ref.put("requestTypeList", requestTypeList);
		
		return ref;
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		int requestTypeMaxNo = requestTypeS.getRequestTypeMaxNo();
		String requestTypeString = ServletRequestUtils.getStringParameter(request, "requestTypeString", "");
		
		requestTypeS.insertRequestType(requestTypeMaxNo + 1, requestTypeString);
		
		return super.onSubmit(request, response, command, errors);
	}
	
}
